<template>
  <el-date-picker
    v-bind="$attrs"
    v-model="dateValue"
    :type="type"
    :placeholder="placeholder"
    :disabled="disabled"
    :clearable="clearable"
    :size="size"
    :readonly="readonly"
    :editable="editable"
    :format="format"
    :value-format="valueFormat"
    :unlink-panels="unlinkPanels"
    :range-separator="rangeSeparator"
    :start-placeholder="startPlaceholder"
    :end-placeholder="endPlaceholder"
    :default-value="defaultValue"
    :default-time="defaultTime"
    :is-range="isRange"
    :arrow-control="arrowControl"
    :validate-event="validateEvent"
    :shortcuts="shortcuts"
    :disabled-date="disabledDate"
    :cell-class-name="cellClassName"
    :teleported="teleported"
    :popper-class="popperClass"
    :popper-style="popperStyle"
    :prefix-icon="prefixIcon"
    :clear-icon="clearIcon"
    :suffix-icon="suffixIcon"
    @change="handleChange"
    @blur="handleBlur"
    @focus="handleFocus"
    @calendar-change="handleCalendarChange"
    @panel-change="handlePanelChange"
    @visible-change="handleVisibleChange"
  />
</template>

<script>
export default {
  name: 'MyElDatePicker',
  inheritAttrs: false,
  props: {
    modelValue: {
      type: [Date, Array, String, Number],
      default: null
    },
    type: {
      type: String,
      default: 'date',
      validator: (value) => {
        return ['date', 'datetime', 'datetimerange', 'daterange', 'month', 'monthrange', 'year', 'yearrange', 'week', 'weeks'].includes(value)
      }
    },
    placeholder: {
      type: String,
      default: '请选择日期'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: 'default',
      validator: (value) => {
        return ['large', 'default', 'small'].includes(value)
      }
    },
    readonly: {
      type: Boolean,
      default: false
    },
    editable: {
      type: Boolean,
      default: true
    },
    format: {
      type: String,
      default: 'YYYY-MM-DD'
    },
    valueFormat: {
      type: String,
      default: undefined
    },
    unlinkPanels: {
      type: Boolean,
      default: false
    },
    rangeSeparator: {
      type: String,
      default: '至'
    },
    startPlaceholder: {
      type: String,
      default: '开始日期'
    },
    endPlaceholder: {
      type: String,
      default: '结束日期'
    },
    defaultValue: {
      type: [Date, Array],
      default: undefined
    },
    defaultTime: {
      type: [Date, Array],
      default: undefined
    },
    isRange: {
      type: Boolean,
      default: false
    },
    arrowControl: {
      type: Boolean,
      default: false
    },
    validateEvent: {
      type: Boolean,
      default: true
    },
    shortcuts: {
      type: Array,
      default: () => []
    },
    disabledDate: {
      type: Function,
      default: undefined
    },
    cellClassName: {
      type: Function,
      default: undefined
    },
    teleported: {
      type: Boolean,
      default: true
    },
    popperClass: {
      type: String,
      default: ''
    },
    popperStyle: {
      type: Object,
      default: () => ({})
    },
    prefixIcon: {
      type: [String, Object],
      default: 'Calendar'
    },
    clearIcon: {
      type: [String, Object],
      default: 'Close'
    },
    suffixIcon: {
      type: [String, Object],
      default: ''
    }
  },
  emits: ['update:modelValue', 'change', 'blur', 'focus', 'calendar-change', 'panel-change', 'visible-change'],
  computed: {
    dateValue: {
      get() {
        return this.modelValue
      },
      set(value) {
        this.$emit('update:modelValue', value)
      }
    }
  },
  methods: {
    handleChange(value) {
      this.$emit('change', value)
    },
    handleBlur(event) {
      this.$emit('blur', event)
    },
    handleFocus(event) {
      this.$emit('focus', event)
    },
    handleCalendarChange(date) {
      this.$emit('calendar-change', date)
    },
    handlePanelChange(date, mode) {
      this.$emit('panel-change', date, mode)
    },
    handleVisibleChange(visible) {
      this.$emit('visible-change', visible)
    }
  }
}
</script> 